package com.seecen.HJM.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seecen.HJM.model.dto.UserDTO;
import com.seecen.HJM.model.entity.Song;
import com.seecen.HJM.model.entity.User;
import com.seecen.HJM.model.entity.UserFavorite;
import com.seecen.HJM.model.vo.UserVO;
import com.seecen.HJM.result.DataVo;
import com.seecen.HJM.service.IUserService;
import com.seecen.HJM.service.SongService;
import com.seecen.HJM.service.UserFavoriteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/")
@RequiredArgsConstructor
public class UserController {
    private final SongService songService;
    private final UserFavoriteService userFavoriteService;
    private final IUserService userService;
    //获取当前用户喜欢的歌曲
    @GetMapping("getLikeSongs")
    public DataVo<IPage<Song>> getLikeSongs(@RequestParam(defaultValue = "1")Integer page,
                                            @RequestParam(defaultValue = "100")Integer size,
                                            @RequestParam(required = false)String name) {

        log.info("搜索喜欢歌曲，页码: {}, 大小: {}, 搜索名称: {}", page, size, name);

        // 获取用户ID（暂时写死为126，后续可以改为从token获取）
        LambdaQueryWrapper<UserFavorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFavorite::getUserId,126);

        List<Long>ids=new ArrayList<>();
        List<UserFavorite> list = userFavoriteService.list(queryWrapper);
        for (UserFavorite userFavorite : list) {
            Long id=userFavorite.getSongId();
            if (id!=null){
                ids.add(id);
            }
        }

        IPage<Song>page1=new Page<>(page,size);
        QueryWrapper<Song> queryWrapper1 = new QueryWrapper<>();
        //在喜欢的音乐id集合范围内
        queryWrapper1.in("s.id",ids);

        // 修复搜索条件：当name不为空且不为空字符串时，才添加搜索条件
        if (StringUtils.hasText(name)) {
            queryWrapper1.like("s.name", name); // 使用like进行模糊搜索
            log.info("添加搜索条件，歌曲名称包含: {}", name);
        }

        page1=songService.getLikedSongsPage(page1,queryWrapper1);

        log.info("搜索完成，找到 {} 条记录", page1.getRecords().size());

        return DataVo.success(page1);
    }

    /**
     * 获取用户信息
     *
     * @return 结果
     */
    @GetMapping("getUserInfo")
    public DataVo<UserVO> getUserInfo() {
        return userService.userInfo();
    }


    @PutMapping("/update")
    public DataVo<?> updateUser(@RequestBody User user) {

        System.out.println("更新用户信息：" + user); // 添加日志
        System.out.println("用户头像：" + user.getUserAvatar()); // 检查头像字段
        boolean success = userService.updateUser(
                user.getUserId(),
                user.getUsername(),
                user.getUserAvatar(),
                user.getPhone(),
                user.getPassword(),
                user.getEmail(),
                user.getUserStatus(),
                user.getUpdateTime(),
                user.getIntroduction()
        );
        return success ? DataVo.success("更新成功") : DataVo.error("更新失败");
    }
}